#include "profile.h"
#include "bbs.h"
#include "byhh.h"
//#include <QtGlobal>

int supportBBS::MaxTypeNumber = 0;
QString supportBBS::SupportTypes[] = {QObject::tr("byhh.net")};

BBS * supportBBS::type2BBS(int type, Profile * profile){
    switch(type){
	default:
	case BYHH:
	    return new byhh(profile);
	    break;
    }
}

baseOnProfile::baseOnProfile(){
    profile = 0;
}

int baseOnProfile::setProfile(Profile * newProfile){
    if (newProfile == NULL) return 1;
    profile = newProfile;
    return 0;
}

Profile * baseOnProfile::getProfile() const{
    return profile;
}

Profile::~Profile(){
    if (!bbs)
	delete bbs;
}

//默认情况下，Profile会建立一个BYHH的数据库
Profile::Profile(QString newName ){
    bbs = 0;
    type = -1;
    name = newName;
    username = "";
    password = "";
    setType(supportBBS::BYHH);
    id = QString::number(qrand());
}

void Profile::setType(int newType){
    if (newType == type) return;
    DEBUG_F();
    type = newType;
    if (bbs != 0)
	delete bbs;///can this kill the memory ... ?
    bbs = supportBBS::type2BBS(newType, this);
}

int Profile::getType() const{
    return type;
}

void Profile::save(QSettings &settings){
    //qDebug() << ">> Saving  profile (id =" << id << ")";
    QString cookie = getCookies(true);
    settings.beginGroup(id);
    settings.setValue("name", name);
    settings.setValue("username", username);
    settings.setValue("password", password);
    settings.setValue("cookie", cookie);
    Options::save(settings);
    settings.endGroup();
}

void Profile::restore(QSettings &settings){
    //qDebug() << ">> Restore profile (id =" << id << ")";
    QString cookie;
    settings.beginGroup(id);
    name = settings.value("name", "newProfile").toString();
    bbs -> name = name;
    username = settings.value("username", "").toString();
    password = settings.value("password", "").toString();
    cookie = settings.value("cookie", "").toString();
    Options::restore(settings);
    settings.endGroup();
    updateCookie(cookie);
}

void Profile::erase(QSettings& settings){
    settings.beginGroup(id);
    Options::erase(settings);
    settings.endGroup();
    settings.remove(id);
}

bool Profile::checkLogin(){
    return (username.toLower() == cookies.value("utmpuserid").toLower());
}

QString Profile::getCookies(bool fullFormat) const{
    QString cookie;
    QMapIterator<QString, QString> i(cookies);
    while (i.hasNext()){
	i.next();
	QString temp;
	temp += i.key();
	temp += "=";
	temp += i.value();
	if (fullFormat)
	    cookie += QString("document.cookie='%1';").arg(temp);
	else
	    cookie += temp + ";";
	
    }
    return cookie;
}

void Profile::updateCookie(QString &html){
    if (html == "") return;
    int pos = 0;
    QRegExp exp("document.cookie='([^=]+)=([^']+)'");
    QString key , value;
    while ( (pos = exp.indexIn(html , pos)) != -1){
	key = exp.cap(1);
	value = exp.cap(2);
	cookies[key] = value;
	pos += exp.matchedLength();
    }
}

BBS* Profile::getBBS() const{			return bbs;}

inline QString Profile::getId() const{		return id;}

inline QString Profile::getName() const{	return name;}

inline QString Profile::getUsername() const{	return username;}

inline QString Profile::getPassword() const{	return password;}

bool Profile::setId( QString value){
    id = value;
    return true;
}

bool Profile::setName( QString value){
    name = value;
    bbs->name = value;
    return true;
}

bool Profile::setUsername( QString value){
    username = value;
    return true;
}

bool Profile::setPassword( QString value){
    password = value;
    return true;
}
